/**
 * @author ZhongJiang
 */

/**
 * @function 核对注册信息
 * @returns {boolean}
 */

var regist_email_ok;
var regist_code_ok;

function checkRegistInfo() {
	if (typeof(regist_email_ok) == "undefined") {
		checkUserRegistEmail();
	}
	
	if (typeof(regist_code_ok) == "undefined") {
		doCheck();
	}
	
	if( !checkUserRegistNickname() || !regist_email_ok
			|| !checkUserRegistPassword() || !checkUserReRegistPassword()
			|| !checkUserRegistSex() || !checkUserRegistBirthday() || !regist_code_ok  
			) {
		return false;
	}
	return true;
}

function ToUserInfoRegist(){
	$("#outer").css("display",'none');
}

/**
 * @function 核对注册邮箱
 * @returns {Boolean}
 */
function checkUserRegistEmail() {
	var userRegistEmail = $('#userRegistEmail').val();

	if(!validateInput(userRegistEmail,3,30,"E")) {
		$('#userRegistEmailError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_USERID_FORMAT_IS_NOT_VALID+'</font>');
		return false;
	}
	
	$.ajax({ 
		url: 'ajaxCheckUserRegistEmailExist',
		data: {
			'userRegistEmail':userRegistEmail
		},
		type: 'post',
		async: true,
		dataType: 'json',
		success: function(res) {
			if(!res) {
				$('#userRegistEmailError').html('<font style="color: green;">'+ValidMsgConstants.VALID_INPUT+'</font>');
				regist_email_ok = true;
			}else {
				$('#userRegistEmailError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_REGIST_MAIL+'</font>');
				regist_email_ok = false;
			}
		}
	});
	return regist_email_ok;
}

/**
 * @function 核对注册昵称
 * @returns {Boolean}
 */
function checkUserRegistNickname() {
	var userRegistNickname = $('#userRegistNickname').val();
	if(isEmpty(userRegistNickname)) {
		$('#userRegistNicknameError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_INPUT_IS_NOT_ALLOW_NULL+'</font>');
		return false;
	}
	if(!validateInput(userRegistNickname, 2, 10, "0aAC")) {
		$('#userRegistNicknameError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_USERNICKNAME_FORMAT_IS_NOT_VALID+'</font>');
		return false;
	}
	$('#userRegistNicknameError').html('<font style="color: green;">'+ValidMsgConstants.VALID_INPUT+'</font>');
	return true;
}

/**
 * @function 核对注册密码
 * @returns {Boolean}
 */
function checkUserRegistPassword() {
	var userRegistPassword = $('#userRegistPassword').val();
	if(isEmpty(userRegistPassword)) {
		$('#userRegistPasswordError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_INPUT_IS_NOT_ALLOW_NULL+'</font>');
		return false;
	}
	if(!validateInput(userRegistPassword, 6, 20, "0aA")) {
		$('#userRegistPasswordError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_USERPASSWORD_FORMAT_IS_NOT_VALID+'</font>');
		return false;
	}
	$('#userRegistPasswordError').html('<font style="color: green;">'+ValidMsgConstants.VALID_INPUT+'</font>');
	return true;
}

/**
 * @function 核对注册确认密码
 * @returns {Boolean}
 */
function checkUserReRegistPassword() {
	var userRegistPassword = $('#userRegistPassword').val();
	var userReRegistPassword = $('#userReRegistPassword').val();
	
	if(isEmpty(userReRegistPassword)) {
		$('#userReRegistPasswordError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_INPUT_IS_NOT_ALLOW_NULL+'</font>');
		
		return false;
	}
	if(userRegistPassword != userReRegistPassword) {
		$('#userReRegistPasswordError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_USERREPASSWORD_IS_NOT_VALID+'</font>');
		$('#userReRegistPassword').val("");
		return false;
	}
	$('#userReRegistPasswordError').html('<font style="color: green;">'+ValidMsgConstants.VALID_INPUT+'</font>');
	return true;
}

/**
 * @function 核对注册性别
 * @returns {Boolean}
 */
function checkUserRegistSex() {
	var userRegistSex = $('#userRegistSex').val();
	return true;
}

/**
 * @function 核对注册生日
 * @returns {Boolean}
 */
function checkUserRegistBirthday() {
	var userRegistBirthday = $('#userRegistBirthday').val();
	var regist = /^\d{4}-\d{2}-\d{2}$/;
	if(!(regist.test(userRegistBirthday)))
	{
		$('#userReRegistBirthdayError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_REGIST_BIRTHDAY+'</font>');
		return false;
	}
	$('#userReRegistBirthdayError').html('<font style="color: green;">'+ValidMsgConstants.VALID_INPUT+'</font>');
	return true;
}
/**
 * @function 核对登录信息
 * @returns {Boolean}
 */
function checkLoginInfo() {
	if(!checkUserLoginEmail() || !checkUserLoginPassword()) {
		$('#userLoginEmailError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_PLEASE_CHECK_ALL_LOGIN_INPUT+'</font>');
		return false;
	}else {
		return true;
	}
}

/***
 * 
 * 上传头像层控制
 * 
 * ****/
function uploadImage(meg){
	$("#"+meg).css("display","block");
}

/**
 * @function 核对登录邮箱
 * @returns {Boolean}
 */
function checkUserLoginEmail() {
	var userLoginEmail = $('#userLoginEmail').val();
	if(isEmpty(userLoginEmail)) {
		$('#userLoginEmailError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_INPUT_IS_NOT_ALLOW_NULL+'</font>');
		document.getElementById("userLoginEmail").className= "error";
		return false;
	}

	if(!validateInput(userLoginEmail, 3, 30, "E")) {
		$('#userLoginEmailError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_USERID_FORMAT_IS_NOT_VALID+'</font>');
		document.getElementById("userLoginEmail").className = "error";
		return false;
	}
	document.getElementById("userLoginEmail").className = "norml";
	$('#userLoginEmailError').html("");
	return true;
}


/**
 * @function 核对登录密码
 * @returns {Boolean}
 */
function checkUserLoginPassword() {
	var userLoginPassword = $('#userLoginPassword').val();
	if(checkUserLoginEmail()){
	if(isEmpty(userLoginPassword)) {
		$('#userLoginEmailError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_INPUT_IS_NOT_ALLOW_NULL+'</font>');
		document.getElementById("userLoginPassword").className = "error";
		return false;
	}
	if(!validateInput(userLoginPassword, 6, 20, "0aA")) {
		$('#userLoginEmailError').html('<font style="color: red;">'+ErrorMsgConstants.ERR_USERPASSWORD_FORMAT_IS_NOT_VALID+'</font>');
		document.getElementById("userLoginPassword").className = "error";
		return false;
	}
	$('#userLoginEmailError').html("");
	document.getElementById("userLoginPassword").className = "norml";
	return true;
	}
	else{
		checkUserLoginEmail();
	}
}
/*
 * 
 * 验证验证码
 * 
 * */
function doCheck(){
	var userRegistcheck = $('#yancheck').val();
	if(isEmpty(userRegistcheck)){
		$("#docheck").html("<font color='red'>"+ErrorMsgConstants.ERR_CHECK+"</font>");
		regist_code_ok = false;
		return regist_code_ok;
	}
	$.ajax({ 
		url: 'ajaxuserRegistcheck',
		data: {
			'userRegistyancheck':userRegistcheck
		},
		
		type: 'post',
		async: false,
		dataType: 'json',
		success: function(res) {
			if(res)
				{
				$("#docheck").html("<font color='green'>"+ValidMsgConstants.VALID_INPUT+"</font>");
				regist_code_ok = true;
				}
			else{
				$("#docheck").html("<font color='red'>"+ErrorMsgConstants.ERR_CHECK+"</font>");
				img();
				regist_code_ok = false;
			}
			
		}
	});
	return regist_code_ok;
}

/*
 * 
 * 层
 * */
function openCeng(meg){
	document.getElementById(meg).style.display = "block";
	}
function closeCeng(meg){
	document.getElementById(meg).style.display = "none";
	}
/*
 * 个性签名：
 * 
 * */
function checkStatement(){
	var len =  jQuery.trim($("#statement").val()).length;
	if(len < 0 || len > 30){
		return false;
	}
	return true;
}
